<?php

/**
 * Created by PhpStorm.
 * User: Jacky Yu
 * Date: 2017/3/21
 * Time: 16:51
 */
class Visitlog_model extends CI_Model
{
    var $_table_name = 'visit_log';

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('cookie');
    }

    //添加游戏访问记录
    /**
     * @param string $user_openid
     * @param array $game
     * @param string $channel
     * @return bool
     */
    public function add_log($user_openid, $game, $channel = 'list')
    {
        $this->log_to_cookie($user_openid, $game);
        $data = array(
            'user_openid' => $user_openid,
            'gameid'      => $game['gameid'],
            'channel'     => $channel,
            'visit_time'  => time(),
            'visit_time_of_hour' => date('H',time())
        );
        $rs = $this->db->insert($this->_table_name, $data);

        return empty($rs) ? false : $this->db->insert_id();
    }

    //删除游戏访问记录todo
    //读取某个用户最近的游戏访问记录
    /**
     * @param $user_openid
     * @param int $num
     * @return array
     */
    public function get_history($user_openid, $num = 16)
    {

        if (get_cookie($user_openid)) {
            return json_decode(get_cookie($user_openid), true);

        } elseif (strlen($user_openid) >= 28) {//非微信openid长度
            $i = 0; //历史记录数据计数器
            $arr_temp = array();
            $sql = "SELECT a.gameid,b.title,b.picture,a.visit_time 
                    FROM 17koko_{$this->_table_name} AS a,17koko_game_hot AS b 
                    WHERE a.user_openid='{$user_openid}' AND a.gameid=b.id  
                    ORDER BY a.id DESC 
                    LIMIT 100";
            $query = $this->db->query($sql);

            while ($i < $num && $row = $query->unbuffered_row()) {

                if (!isset($arr_temp[$row->gameid])) {
                    $arr_temp['a' . $row->gameid]['gameid'] = $row->gameid;
                    $arr_temp['a' . $row->gameid]['title'] = $row->title;
                    $arr_temp['a' . $row->gameid]['picture'] = $row->picture;
                    $arr_temp['a' . $row->gameid]['visit_time'] = $row->visit_time;
                    $i++;
                }
            }

            set_cookie($user_openid, json_encode($arr_temp), '604800');

            return $arr_temp;

        } else {
            return array();
        }

    }

    //追加游戏访问记录到cookie
    /**
     * @param string $user_openid 用户openid
     * @param array $game 游戏对象数组
     * @param int $num 访问记录的最大数量
     */
    public function log_to_cookie($user_openid, $game = array(), $num = 16)
    {


        if (is_array($game) && !empty($game)) {
            $arr_log = $this->get_history($user_openid);
            $game_key = "a{$game['gameid']}";//拼接游戏数组引索key

            if (isset($arr_log[$game_key])) {
                unset($arr_log[$game_key]);
            }

            $game['visit_time'] = time();
            $arr_new = array($game_key => $game);
            $arr_log = array_merge($arr_new, $arr_log);

            if (count($arr_log) > $num) {
                array_pop($arr_log);
            } //当游戏历史记录超过最大数量，释放数组最后一个元素

            set_cookie($user_openid, json_encode($arr_log), '604800');
        }
    }

    /**
     * 每天每个游戏的点击数
     * @param int $day
     */
    public function summarize_day_click_by_game($day=0){
        $day===0 && $day=strtotime(date('Y-m-d',time()));

        $sql = "select gameid,count(*) as `count`,($day) as summary_time 
                 from 17koko_visit_log
                 where visit_time >=$day and visit_time < $day+24*3600 
                 group by gameid";

        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 各个渠道每日的点击数
     * @param int $day
     */
    public function summarize_day_click_by_channel($day=0){
        $day===0 && $day=strtotime(date('Y-m-d',time()));

        $sql = "select channel,count(*) as `count`,($day) as summary_time 
                 from 17koko_visit_log
                 where visit_time >=$day and visit_time < $day+24*3600 
                 group by channel";

        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 每天各个小时的总访问量
     * @param int $day
     */
    public function summarize_hour_click_by($day=0){
        $day===0 && $day=strtotime(date('Y-m-d',time()));

        $sql = "select visit_time_of_hour as hour_of_day,count(*) as `count`,($day) as summary_time 
                 from 17koko_visit_log
                 where visit_time >=$day and visit_time < $day+24*3600 
                 group by visit_time_of_hour";

        $query = $this->db->query($sql);
        return $query->result_array();
    }
}
